<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <!-- -------------写在这里------------ -->

      <el-table :data="list" style="width: 100%" stripe>
        <el-table-column prop="stem" label="标题"> </el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="游览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="180px">
        </el-table-column>
        <el-table-column prop="createdAt" label="操作" width="120px">
          <template #default="{ row }">
            <i class="el-icon-view" @click="openDrawer('view', row.id)"
              >&nbsp;&nbsp;&nbsp;&nbsp; </i
            ><i
              class="el-icon-edit-outline"
              @click="openDrawer('edit', row.id)"
            >
              &nbsp;&nbsp;&nbsp;&nbsp;</i
            ><i class="el-icon-delete" @click="del(row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <!-- //分页页码 -->
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="pageSize"
        layout=" prev, pager, next , total"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      :title="titleDrawer"
      size="50%"
      :visible.sync="isShowDrawer"
      :before-close="handleClose"
    >
      <!-- 抽屉的内容区 -->
      <div v-if="typeDrawer === 'view'" id="drawer1">
        <h2>{{ form.stem }}</h2>
        <p v-html="form.content"></p>
      </div>
      <!-- //===================== -->
      <el-form
        v-else
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content"></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onAdd">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import {
  articleList,
  createArticle,
  removeArticle,
  getArticle,
  updateArticle
} from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      list: [], // 接收接口返回的数据
      total: 0, // 准备存储的总计多少条
      current: 1, // 页码,默认获取第一页
      pageSize: 10,
      isShowDrawer: false,
      typeDrawer: '',
      form: {
        stem: '',
        content: ''
      },

      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经标题', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.initData()
  },
  methods: {
    async initData () {
      const res = await articleList({
        current: this.current,
        pageSize: this.pageSize
      })

      this.list = res.data.rows
      this.total = res.data.total
    },
    // 分页方法
    // val是表示点击的页码
    handleCurrentChange (val) {
      this.current = val
      this.initData()
    },
    // 打开抽屉
    async openDrawer (type, id) {
      this.isShowDrawer = true
      this.typeDrawer = type
      if (type !== 'add') {
        const res = await getArticle(id)
        // console.log(res)
        this.form = {
          ...res.data
        }
      }
    },
    // 关闭抽屉之前做什么
    handleClose () {
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      this.isShowDrawer = false
    },
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示')
        .then(async () => {
          await removeArticle(id)
          this.initData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {})
    },
    async onAdd () {
      if (this.typeDrawer === 'add') {
        const { stem, content } = this.form
        await createArticle({ stem, content })
        this.$message.success('添加成功')
      } else {
        console.log(this.form)
        const { stem, content, id } = this.form
        await updateArticle({ stem, content, id })
        this.$message.success('修改成功')
      }

      this.handleClose()
      this.current = 1
      this.initData()
    }
  },
  computed: {
    titleDrawer () {
      let title = '标题'
      if (this.typeDrawer === 'add') title = '添加面经'
      if (this.typeDrawer === 'view') title = '面经预览'
      if (this.typeDrawer === 'edit') title = '修改面经'
      return title
    }
  },
  components: {
    quillEditor
  }
}
</script>

<style lang="scss" scoped>
#drawer1 {
  padding-left: 50px;
}
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
